//
//  TimeSettingView.m
//  TaiHao
//
//  Created by rzm on 16/6/2.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "SampuxInstallTimeView.h"
//#import "TimeTableViewCell.h"
#import "SmapuxTimeViewListCell.h"
#import "SampuxInstallTimeModel.h"
#import "SampuxInstallDetailTimeModel.h"
#import "UIScrollView+MJRefresh.h"

@interface SampuxInstallTimeView ()<UITableViewDelegate,UITableViewDataSource>
{
    NSArray *speedArr;
}
//@property(nonatomic,strong) NSIndexPath *indexPath;

@end

@implementation SampuxInstallTimeView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = THSystemClearColor;
        _modelArr = [NSMutableArray array];
        [self setupSubView];
    }
    return self;
}

- (void)setupSubView
{
    _installTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, self.height - 64) style:UITableViewStylePlain];
    _installTableView.backgroundColor = THSystemClearColor;
    [self addSubview:_installTableView];
    _installTableView.delegate = self;
    _installTableView.dataSource = self;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reciveTimeNotification:) name:@"THNotificationCenter_settingTime" object:nil];
//    [self getDataSource];
    _installTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _header = [MJRefreshStateHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    _installTableView.mj_header = _header;
    [_header beginRefreshing];
}

- (void)refreshData
{
//    [GBInterMaster_CommForDeivce requestShowProgramInfo:0 withDeviceAccount:self.model.deviceAccount];
    if (self.listModel != nil) {
        [GBInterMaster_CommForDeivce requestShowProgramInfo:0 withDeviceAccount:self.listModel.accountStr];
    }else
    {
        [_header endRefreshing];
     }

}

- (void)setModelArr:(NSMutableArray *)modelArr
{
    dispatch_async(dispatch_get_main_queue(), ^{
        _modelArr = modelArr;
        [_installTableView reloadData];
        [_header endRefreshing];
    });
}

#pragma mark UITableViewCellDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SmapuxTimeViewListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"timeCell"];
    if (cell == nil) {
        cell = [[SmapuxTimeViewListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"timeCell"];
    }
    cell.backgroundColor = THSystemClearColor;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.tag = indexPath.row + 10;
    NSArray *weekArr = @[@"周一",@"周二",@"周三",@"周四",@"周五",@"周六",@"周日"];
    SampuxInstallTimeModel *model = self.modelArr[indexPath.row];
    cell.weeklab.text = weekArr[indexPath.row];
    cell.model = model;
    return cell;
}

#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return Sampux6HScale(206);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SmapuxTimeViewListCell *cell = [_installTableView cellForRowAtIndexPath:indexPath];
    cell.subView.backView.backgroundColor = THSystemClearColor;
    
    if (self.timeDelegate && [self.timeDelegate respondsToSelector:@selector(timeSettingSelectedCellSub: andSubData:)]) {
        SampuxInstallTimeModel *model = _modelArr[indexPath.row];
        [self.timeDelegate timeSettingSelectedCellSub:indexPath.row andSubData:model];
        NSLog( @"onePoint == %@",model.onePoint);
    }
    cell.backgroundColor = THSystemClearColor;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SmapuxTimeViewListCell *cell = [_installTableView cellForRowAtIndexPath:indexPath];
    cell.subView.backView.backgroundColor = [UIColor clearColor];
}

- (void)reciveTimeNotification:(NSNotification *)notification
{
    SampuxInstallDetailTimeModel *model = notification.object;
    NSArray *btnArr = model.selectedBtnArr;
    
    for (int i = 0 ; i < btnArr.count; i++) {
        UIButton *btn = btnArr[i];
        NSInteger indexTag = btn.tag;
        if (indexTag == 10000) {
            indexTag = btn.tag - 10000 + 6;
        }else if (btn.tag == 10007)
        {
           indexTag = btn.tag - 10000;
        }else
        {
            indexTag = btn.tag - 10000 - 1;
        }
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:indexTag inSection:0];        
        SmapuxTimeViewListCell *cell = [_installTableView cellForRowAtIndexPath:indexPath];
        NSInteger type = model.selectedCellType;
        SampuxTimeViewListCellSub *sub = [cell viewWithTag:model.selectedCellType + 100];
        SampuxInstallTimeModel *timeModel = cell.model;
        
        if (type == 0) {
            timeModel.oneValue = model.speedStr ;
             timeModel.onePoint = model.timeStr;
        }else if (type == 1)
        {
            timeModel.twoValue = model.speedStr;
            timeModel.twoPoint = model.timeStr;
        }else if (type == 2)
        {
            timeModel.threeValue = model.speedStr;
            timeModel.threePoint = model.timeStr;
        }else
        {
            timeModel.threeValue = model.speedStr;
            timeModel.threePoint = model.timeStr;
        }
        cell.model = timeModel;
    }
}

@end
